package zy.test;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.serialport.JNI.SerialPort;
import android.util.Log;


import java.io.File;
import java.io.InputStream;

@SuppressLint({"HandlerLeak"})
public class ScanGunThread extends Thread {
    private Handler handler = null;
    private Listener listener=null;
    private static InputStream fileinput = null;
    public ScanGunThread(){
        try {
            String  release=android.os.Build.VERSION.RELEASE;
            SerialPort serialPort=null;
            if(release.equals("6.0.1"))
                serialPort = new SerialPort(new File("/dev/ttyS0"), 9600, 8, 1, "N"); //新白色机子
            if(release.equals("4.4.4"))
                serialPort = new SerialPort(new File("/dev/ttyS1"), 9600, 8, 1, "N"); //旧白色机子
            ////SerialPort serialPort= new SerialPort(new File("/dev/ttyS1"), 38400, 8, 1, "N");
            fileinput=serialPort.getInputStream();
            this.handler=new Handler(){
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    listener.onListener((Result)msg.obj);
                }
            };
        } catch (Exception e) {
        }
    }
    private void kkk(){
        if(fileinput==null)
            return;
        int size=0;
        byte[] buffer=new byte[18];
        StringBuffer strBuffer = new StringBuffer();
        while(true){
            try {
                Thread.sleep(300);
                size = fileinput.read(buffer, 0, 18);
                if(size>0) {
                    do {
                        strBuffer.append(new String(buffer, 0, size, "GBK"));
                        Thread.sleep(50);
                        size = fileinput.read(buffer, 0, 18);
                    }while(size>0);
                    String str = strBuffer.substring(0, strBuffer.indexOf("\r"));
                    strBuffer.setLength(0);
                    Log.i("hello","code:"+str);
                    Result  result=null;
                    switch(str.length()) {
//                        case 12:
//                            result=new Result(Result.Flag.MYSHOP,str);
//                            break;
                        case 13:
                            result=new Result(Result.Flag.STDSHOP,str);
                            break;
                        case 18:
                            if(str.startsWith("0",0))
                                result=new Result(Result.Flag.MYSHOP,str);
                            if(str.startsWith("1",0))
                                result=new Result(Result.Flag.WECHAT,str);
                            if(str.startsWith("2",0))
                                result=new Result(Result.Flag.ALIPAY,str);
                            break;
                        default:
                            break;
                    }
                    Message msg = new Message();
                    msg.obj =result;
                    ScanGunThread.this.handler.sendMessage(msg);
                }
            } catch(Exception e){}
        }

    }

    public void run() {
        super.run();
        kkk();
    }
    public interface Listener {
        void onListener(Result result);
    }
    public void  setListener(Listener  listener){
        this.listener=listener;
    }
}
